package cn.cdu.medical.controller;

import cn.cdu.basics.baseVo.Result;
import cn.cdu.basics.utils.ResultUtil;
import cn.cdu.medical.dto.PrescriptionDTO;
import cn.cdu.medical.service.IPrescriptionService;
import cn.cdu.medical.vo.PrescriptionVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
//  import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/zwz/prescription")
@Tag(name = "处方管理接口")
public class PrescriptionController {

    @Resource
    private IPrescriptionService prescriptionService;

    @PostMapping
    @Operation(summary = "创建处方")
    public Result<String> createPrescription(@Validated @RequestBody PrescriptionDTO prescriptionDTO) {
        log.info("创建处方请求，参数：{}", prescriptionDTO);
        String id = prescriptionService.createPrescription(prescriptionDTO);
        return ResultUtil.data(id);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取处方详情")
    public Result<PrescriptionVO> getPrescriptionDetail(
            @Parameter(description = "处方ID") @PathVariable String id) {
        log.info("查询处方详情，处方ID：{}", id);
        PrescriptionVO prescription = prescriptionService.getPrescriptionById(id);
        return ResultUtil.data(prescription);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询处方列表")
    public Result<IPage<PrescriptionVO>> getPrescriptionPage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNumber,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "患者ID") @RequestParam(required = false) String patientId,
            @Parameter(description = "医生ID") @RequestParam(required = false) String doctorId,
            @Parameter(description = "处方状态") @RequestParam(required = false) Integer status) {
        log.info("分页查询处方列表，参数：pageNumber={}，pageSize={}，patientId={}，doctorId={}，status={}",
                pageNumber, pageSize, patientId, doctorId, status);
        
        IPage<PrescriptionVO> page = prescriptionService.getPrescriptionPage(
                new Page<>(pageNumber, pageSize), patientId, doctorId, status);
        return ResultUtil.data(page);
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新处方状态")
    public Result<Boolean> updatePrescriptionStatus(
            @Parameter(description = "处方ID") @PathVariable String id,
            @Parameter(description = "状态") @RequestParam Integer status) {
        log.info("更新处方状态，处方ID：{}，状态：{}", id, status);
        boolean success = prescriptionService.updatePrescriptionStatus(id, status);
        return ResultUtil.data(success);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "作废处方")
    public Result<Boolean> invalidatePrescription(
            @Parameter(description = "处方ID") @PathVariable String id) {
        log.info("作废处方，处方ID：{}", id);
        boolean success = prescriptionService.invalidatePrescription(id);
        return ResultUtil.data(success);
    }

    @GetMapping("/medical-record/{medicalRecordId}")
    @Operation(summary = "获取病历关联的处方列表")
    public Result<List<PrescriptionVO>> getPrescriptionsByMedicalRecordId(
            @Parameter(description = "病历ID") @PathVariable String medicalRecordId) {
        log.info("查询病历关联的处方列表，病历ID：{}", medicalRecordId);
        List<PrescriptionVO> prescriptions = prescriptionService.getPrescriptionsByMedicalRecordId(medicalRecordId);
        return ResultUtil.data(prescriptions);
    }

    @PutMapping
    @Operation(summary = "修改处方")
    public Result<Boolean> updatePrescription(@Validated @RequestBody PrescriptionDTO prescriptionDTO) {
        log.info("修改处方请求，参数：{}", prescriptionDTO);
        boolean success = prescriptionService.updatePrescription(prescriptionDTO);
        return ResultUtil.data(success);
    }
} 